using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace FoundationLib.Mail 
{
	/// <summary>
	/// This class represents a set of name-value pairs.
	/// </summary>
	public class MailData 
	{
		/// <summary>
		/// The data items
		/// </summary>
		private Dictionary<string, MailDataItem> items;

		/// <summary>
		/// Creates a new mail data instance.
		/// </summary>
		public MailData()
		{
			this.items = new Dictionary<string, MailDataItem>();
		}

		/// <summary>
		/// The object invariant.
		/// </summary>
		[ContractInvariantMethod]
		private void ObjectInvariant()
		{
			Contract.Invariant(this.items != null);
		}


		/// <summary>
		/// Gets the number of items in this data container.
		/// </summary>
		public int ItemCount
		{
			get
			{
				return this.items.Count;
			}
		}

		/// <summary>
		/// Clears the data dictionary.
		/// </summary>
		public void Clear()
		{
			this.items.Clear();
		}

		/// <summary>
		/// Gets or sets the data item with the given key.
		/// </summary>
		/// <returns>The data item with the given key</returns>
		/// <param name="key">The data item key</param>
		/// <exception cref="ArgumentNullException">A null-reference was passed.</exception>
		public MailDataItem this[string key]
		{
			get
			{
				Contract.Requires(!String.IsNullOrWhiteSpace(key));

				if (this.items.ContainsKey(key))
					return (items[key]);
				else
					return null;
			}
			set
			{
				Contract.Requires(value != null);
				Contract.Requires(!String.IsNullOrWhiteSpace(key));

				if (this.items.ContainsKey(key))
					this.items[key] = value;
				else
					this.items.Add(key, value);
			}
		}

	}

}